import 'package:pornhub_app/components/app_bg_view.dart';
import 'package:pornhub_app/components/easy_toast.dart';
import 'package:pornhub_app/utils/color.dart';
import 'package:pornhub_app/views/mine/frontpage/controller/login_register_controller.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../../../assets/styles.dart';
import '../../../generate/app_image_path.dart';

class LoginRegisterPage extends StatelessWidget {
  const LoginRegisterPage({super.key});

  @override
  Widget build(BuildContext context) {
    final bgColor = Styles.color.bgColor;

    return Scaffold(
      backgroundColor: bgColor,
      appBar: AppBar(
        backgroundColor: Colors.transparent,
        iconTheme: const IconThemeData(color: Colors.white),
        title: Text("账号",
            textAlign: TextAlign.center,
            style: TextStyle(
              color: COLOR.hexColor('#ffffff'),
              fontSize: 16.sp,
              fontWeight: FontWeight.w500,
            )),
        centerTitle: true,
      ),
      body: const LoginRegisterPageState(),
    );
  }
}

class LoginRegisterPageState extends StatefulWidget {
  const LoginRegisterPageState({super.key});

  @override
  State<StatefulWidget> createState() => _LoginRegisterPageState();
}

class _LoginRegisterPageState extends State<LoginRegisterPageState> {
  TextEditingController contentController = TextEditingController();
  TextEditingController contentControllerTwo = TextEditingController();
  final controller = Get.find<LoginRegisterController>();

  void onCLick(int index) {
    switch (index) {
      case 1:
        if (contentController.text.isEmpty) {
          EasyToast.show('请输入账号名称');
          return;
        }
        if (contentControllerTwo.text.isEmpty) {
          EasyToast.show('请输入密码');
          return;
        }
        controller.postLogin(
            contentController.text.trim(), contentControllerTwo.text.trim());
        break;
      case 2:
        if (contentController.text.isEmpty) {
          EasyToast.show('请输入账号名称');
          return;
        }
        if (contentControllerTwo.text.isEmpty) {
          EasyToast.show('请输入密码');
          return;
        }
        controller.postRegister(
            contentController.text.trim(), contentControllerTwo.text.trim());
        break;
    }
  }

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Container(
        padding: EdgeInsets.only(left: 30.w, top: 70.w, right: 30.w),
        width: double.maxFinite,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              "账号",
              style: TextStyle(
                fontSize: 14.sp,
                color: COLOR.hexColor('#ffffff'),
              ),
            ),
            TextField(
              controller: contentController,
              style: TextStyle(
                color: COLOR.hexColor('#ffffff'),
                fontSize: 14.sp,
              ),
              decoration: InputDecoration(
                hintText: '请输入最少6位数账号',
                // helperText: '账号无法修改，建议使用手机号/邮箱等常用信息',
                hintStyle: TextStyle(
                  color: COLOR.hexColor('#999999'),
                  fontSize: 14.sp,
                ),
                helperStyle: TextStyle(
                  color: COLOR.hexColor('#999999'),
                  fontSize: 11.sp,
                ),
                enabledBorder: UnderlineInputBorder(
                  borderSide: BorderSide(
                    color: COLOR.hexColor('#292A31'),
                    width: 1.0,
                  ),
                ),
                focusedBorder: UnderlineInputBorder(
                  borderSide: BorderSide(
                    color: COLOR.hexColor('#292A31'),
                    width: 1.0,
                  ),
                ),
                counter: const SizedBox.shrink(),
              ),
            ),
            Padding(padding: EdgeInsets.only(top: 30.w)),
            Text(
              "密码",
              style: TextStyle(
                fontSize: 14.sp,
                color: COLOR.hexColor('#ffffff'),
              ),
            ),
            TextField(
              controller: contentControllerTwo,
              style: TextStyle(
                color: COLOR.hexColor('#ffffff'),
                fontSize: 14.sp,
              ),
              decoration: InputDecoration(
                hintText: '请输入密码最少6位字符',
                // helperText: '账号无法修改，建议使用手机号/邮箱等常用信息',
                hintStyle: TextStyle(
                  color: COLOR.hexColor('#999999'),
                  fontSize: 14.sp,
                ),
                helperStyle: TextStyle(
                  color: COLOR.hexColor('#999999'),
                  fontSize: 11.sp,
                ),
                enabledBorder: UnderlineInputBorder(
                  borderSide: BorderSide(
                    color: COLOR.hexColor('#292A31'),
                    width: 1.0,
                  ),
                ),
                focusedBorder: UnderlineInputBorder(
                  borderSide: BorderSide(
                    color: COLOR.hexColor('#292A31'),
                    width: 1.0,
                  ),
                ),
                counter: const SizedBox.shrink(),
              ),
            ),
            Padding(padding: EdgeInsets.only(top: 40.w)),
            Flex(
              direction: Axis.horizontal,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                InkWell(
                  highlightColor: Styles.color.bgColor,
                  splashColor: Styles.color.bgColor,
                  onTap: () => onCLick(2),
                  child: Container(
                    width: 160.w,
                    height: 40.w,
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(20),
                      border: Border.all(
                        color: COLOR.playerThemColor,
                        width: 1.0,
                      ),
                    ),
                    child: Flex(
                      direction: Axis.horizontal,
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Text(
                          "注册",
                          style: TextStyle(
                            fontSize: 14.sp,
                            color: COLOR.playerThemColor,
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
                Padding(padding: EdgeInsets.only(left: 16.w)),
                InkWell(
                  highlightColor: Styles.color.bgColor,
                  splashColor: Styles.color.bgColor,
                  onTap: () => onCLick(1),
                  child: AppBgView(
                    width: 160.w,
                    height: 40.w,
                    imagePath: AppImagePath.mine_img_login_btn,
                    child: Flex(
                      direction: Axis.horizontal,
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Text(
                          "登录",
                          style: TextStyle(
                            fontSize: 14.sp,
                            color: Colors.white,
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ],
            ),
            Padding(padding: EdgeInsets.only(top: 30.w)),
            Text(
              "温馨提示：",
              style: TextStyle(
                fontSize: 12.sp,
                color: COLOR.hexColor('#ffffff'),
                fontWeight: FontWeight.w600,
              ),
            ),
            Text(
              "1.账号密码必须是6位以上的字母加数字组合",
              style: TextStyle(
                fontSize: 12.sp,
                color: COLOR.hexColor('#ffffff'),
              ),
            ),
            Text(
              "2.请务必记住自己的账号密码，不提供密码修改和找回",
              style: TextStyle(
                fontSize: 12.sp,
                color: COLOR.hexColor('#ffffff'),
              ),
            ),
            Text(
              "3.请勿把账号分享给别人，如遗失账号平台概不负责",
              style: TextStyle(
                fontSize: 12.sp,
                color: COLOR.playerThemColor,
              ),
            ),
          ],
        ),
      ),
    );
  }
}
